package com.gitee.mrj.aiboot.core.sf;


import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Component
final class GeneratorFactoryLoader {

    @Autowired
    List<FormDefinitionGenerator> defs;


    @PostConstruct
    public void init() {
        Collections.sort(defs, getComparator());
        for (FormDefinitionGenerator def : this.defs) {
            FormDefinitionGeneratorFactory.getInstance().register(def);
        }

    }

    /**
     * 排序规则
     *
     * @return
     */
    private static Comparator<FormDefinitionGenerator> getComparator() {
        return (s1, s2) -> {
            Integer order1 = OrderUtils.getOrder(s1.getClass(), 0);
            Integer order2 = OrderUtils.getOrder(s2.getClass(), 0);
            return NumberUtils.compare(order1, order2);
        };
    }
}
